//
//  UIImage+APMClip.m
//  OC-Example
//
//  Created by lcj on 2020/12/1.
//  Copyright © 2020 apang.men. All rights reserved.
//

#import "UIImage+APMClip.h"
@implementation UIImage (APMClip)
- (UIImage *)apm_clipImageWithImageInRect:(CGRect)rect {
    if (self != nil && !CGRectIsNull(rect) && !CGRectIsEmpty(rect)) {
        CGImageRef cgImage = CGImageCreateWithImageInRect(self.CGImage, rect);
        UIImage * rectImage = [UIImage imageWithCGImage:cgImage];
        CGImageRelease(cgImage);
        return rectImage;
    }
    return self;
}

- (UIImage *)apm_clipImageWithImageInPath:(UIBezierPath *)bezierPath size:(CGSize)size{
    if(self == nil) return self;
    if(bezierPath == nil) return self;
    if (CGSizeEqualToSize(size, CGSizeZero)) return self;
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    [bezierPath addClip];
    [self drawAtPoint:CGPointMake(0, 0)];
    //从上下文中取出图片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    //关闭上下文
    UIGraphicsEndImageContext();
    return newImage;
}
@end
